<template>
  <div class="login-bg">
    <div class="login-wrap">

      <el-form
      ref="form"
      label-width="auto"
      :model="info"
      :label-position="labelPosition"
      :rules="rules"
      >
        <el-form-item label="账号" prop="account">
          <el-input v-model="info.account" placeholder="请输入账号" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="info.password" type="password" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item label="验证码" prop="verify">
          <el-input v-model="info.verify" placeholder="请输入验证码" />
        </el-form-item>

        <el-form-item label=" ">
          <div style="width: 100%; display: flex;justify-content: space-between;">
            <el-button type="success" style="width: 100%;" @click="login(form)" :loading="loading">登录</el-button>
            <el-button type="info" style="width: 100%;" @click="reset(form)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<!-- 登录页 -->
<script setup>
import { reactive, ref, onBeforeUnmount } from 'vue'
import { ElMessage } from 'element-plus';
import { useRouter } from "vue-router";
import systemParams from '@/utils/systemParams.js'
import api from '@/api/getToken'
import { useRouteMenu } from '@/store/menu.store.js'
import { storeToRefs } from 'pinia'
const store = useRouteMenu();
let { menu, bread, openmenu } = storeToRefs(store)

let loading = ref(false)
const router = useRouter();
const labelPosition = ref('right')
const form = ref()
const info = reactive({
  account: 'admin',
  password: '1',
  verify: '1'
})

const rules = reactive(
  { 
    account: [{required: true, message: '请输入账号', trigger: 'blur'}],
    password: [{required: true, message: '请输入密码', trigger: 'blur'}],
    verify: [{required: true, message: '请输入验证码', trigger: 'blur'}],
  }
)

// 请求登录
const toLogin = () => {
  loading.value = true
  api.getToken(info).then(res => {
    if(res && res.token) {
      localStorage.setItem('token', res.token)
      ElMessage.success('身份验证通过! 正在获取菜单!')
      // 跳转
      if(!systemParams.is_need_a_homepage) {
        router.push("/layout");
      } else {
        router.push("/dashboard");
      }
    }
    loading.value = false
  })
}

// 校验
const login = async(formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // 校验通过 发起请求
      toLogin()
    } else {
      ElMessage.warning('请填写完整信息!')
    }
  })
}

const reset = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

const keyup = e => {
  if(e.code === 'Enter') login(form.value)
}

document.addEventListener('keyup', keyup)

// 清除本地缓存
!function(){
  localStorage.removeItem('token')

  menu.value = []
  bread.value = []
  openmenu.value = 1
}(); 


// 卸载键盘事件
onBeforeUnmount(() => {
  document.removeEventListener('keyup', keyup)
})


</script>

<style lang="scss" scoped>
$height: 40px;
.login-bg {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-wrap {
    min-width: 500px;
    width: 500px;
    background-color: #fff;
    border-radius: 10px;
    padding: 40px;
  }
}
:deep(.el-input) {
  width: 100% !important;
  .el-input__wrapper {
    .el-input__inner{
      height: $height;
    }
  }
}
:deep(.el-form-item__label-wrap) {
  .el-form-item__label {
    height: 100%;
    line-height: $height;
    font-weight: 1000;
  }
}
</style>